package com.tanhua.autoconfig.template;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.tanhua.autoconfig.properties.SmsProperties;

public class SmsTemplate {
    private SmsProperties smsProperties;
    public SmsTemplate(SmsProperties smsProperties) {
        this.smsProperties = smsProperties;
    }
    public void sendMsg(String phone,String code){

        try {
            // 1. 配置阿里云 SDK 的认证信息
            Config config = new Config()
                    .setAccessKeyId(smsProperties.getAccessKey()) // 替换为你的 AccessKeyId
                    .setAccessKeySecret(smsProperties.getSecret()); // 替换为你的 AccessKeySecret
            config.endpoint = "dysmsapi.aliyuncs.com"; // 设置短信服务的 endpoint

            // 2. 创建客户端
            Client client = new Client(config);

            // 3. 创建短信发送请求
            SendSmsRequest request = new SendSmsRequest()
                    .setSignName(smsProperties.getSignName()) // 替换为你的短信签名
                    .setTemplateCode(smsProperties.getTemplateCode()) // 替换为你的模板 CODE
                    .setPhoneNumbers(phone) // 替换为接收短信的手机号
                    .setTemplateParam("{\"code\":\""+code+"\"}"); // 替换为模板参数


            // 4. 发送短信
            SendSmsResponse response = client.sendSms(request);
            System.out.println("短信发送成功：" + response.getBody().getMessage());
        } catch (Exception e) {
            System.err.println("短信发送失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
    }

